package com.yanqu.road.dao.impl.match;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.match.MatchInfo;
import com.yanqu.road.entity.config.match.MatchReward;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/2/5 17:53
 * 杂役dao
 */
public class MatchConfigDaoImpl extends TempDao {

    public Map<Integer, List<MatchInfo>> getMatchListMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_variety";
        Map<Integer, List<MatchInfo>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                MatchInfo data = new MatchInfo();
                data.setId(rs.getInt("id"));
                data.setOccupation(rs.getInt("occupation"));
                data.setLevel(rs.getInt("level"));
                data.setUserNumLimit(rs.getInt("userNumLimit"));
                data.setWeight(rs.getInt("weight"));
                data.setConsume(rs.getString("consume"));
                data.setRewardsType(rs.getInt("rewardsType"));
                if (!dataMap.containsKey(data.getLevel())) {
                    dataMap.put(data.getLevel(), new ArrayList<>());
                }
                dataMap.get(data.getLevel()).add(data);
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, List<MatchReward>> getMatchRewardsMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_variety_rewards";
        Map<Integer, List<MatchReward>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                MatchReward data = new MatchReward();
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setPlayerRewards(rs.getString("playerRewards"));
                data.setPatronsRewards(rs.getString("patronsRewards"));
                data.setParam(StringUtils.stringToIntegerList(rs.getString("param"), "\\|"));
                if (!dataMap.containsKey(data.getType())) {
                    dataMap.put(data.getType(), new ArrayList<>());
                }
                dataMap.get(data.getType()).add(data);
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
